package com.spring.campus_express_delivery.interceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;


//https://blog.csdn.net/wmq_fly/article/details/82634287
@Configuration
public class MvcInterceptorConfig extends WebMvcConfigurationSupport{

@Bean
public TokenInterceptor tokenInterceptor(){
    return new TokenInterceptor();
}

    @Override
    protected void addInterceptors(InterceptorRegistry registry) {
        // 多个拦截器组成一个拦截器链
        // addPathPatterns 用于添加拦截规则，/**表示拦截所有请求
        // excludePathPatterns 用户排除拦截
        registry.addInterceptor(tokenInterceptor())//
                .addPathPatterns("/**")// addPathPatterns 用于添加拦截规则
                .excludePathPatterns("/users/web/login","/users/web/register","/users/wx/login","/users/wx/register",
                        "/notices/web/select_notice_id","/notices/web/notice_list","/notices/web/select_notice","/users/web/count","/users/web/select_user_id",
                        "/school_address/wx/serviceAddress_list","/orders/wx/count_order",
                        "/test/web/test_update_info","/test/exceptionTest","/index.html","/favicon.ico","/index","/csrf","/")
                .excludePathPatterns("/static/**")//静态资源  静态访问不到解决方法https://liudongdong.blog.csdn.net/article/details/100412827  https://hanquan.blog.csdn.net/article/details/104095196
                .excludePathPatterns("/swagger-resources/**","/webjars/**","/v2/**","favicon.icon","/swagger-ui.html/**")
                //No mapping for GET /index.html
//                .excludePathPatterns("classpath:/META-INF/resources/", "classpath:/resources/",
//                        "classpath:/static/", "classpath:/public/")
                // springboot拦截器拦截了swagger的解决方法 https://blog.csdn.net/xiaodaopei/article/details/103334130
                .excludePathPatterns("/hello/test","/hello/hello");
        super.addInterceptors(registry);
    }
    /**
     * 解决swagger-ui 无法访问
     * 两个方法都需要重写，只加任何一个都无法生效
     *  https://blog.csdn.net/weixin_42863267/article/details/102976901
     * @param registry
     */
    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {

        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
        //https://blog.csdn.net/HAG_blank/article/details/107205945
//        if (!registry.hasMappingForPattern("/**")) {
//            registry.addResourceHandler("/**").addResourceLocations(
//                    CLASSPATH_RESOURCE_LOCATIONS);
//        }
        //https://blog.csdn.net/niugang0920/article/details/79994153
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");

    }

}
